/*0GI1xcoeligdpMeXoHBphggJgy1ecejSkxa04FXn/MW9FwzMYLMeZK3mD3mLyGPh*/
// 表体行操作列处理
import { ajax, toast } from 'nc-lightapp-front';
import { pageConfig } from '../const';
import ampub from 'ampub';
import { handleCheck_range } from '../../card/events/buttonClick';
const { utils, components } = ampub;
const { multiLangUtils } = utils;
const { getMultiLangByID } = multiLangUtils;
const { ScriptReturnUtils, queryAboutUtils } = components;
const { getScriptListReturn } = ScriptReturnUtils;
const { openListApprove } = queryAboutUtils;
const { tableId, pagecode, pkField, url, cardRouter, pagecodecard } = pageConfig;

export default function tableButtonClick(props, key, text, record, index) {
    // 表格操作按钮
    switch (key) {
        // 删除
        case 'Delete':
            singleDel.call(this, props, record, index);
            break;
            // 提交
        case 'Commit':
            commitClick.call(this, 'SAVE', props, tableId, index, record);
            break;
            // 收回
        case 'UnCommit':
            commitClick.call(this, 'UNSAVE', props, tableId, index, record);
            break;
            // 重新分配盘点人
        case 'ReInventory':
            reInventory.call(this, props, key, record, index);
            break;
            // 审批详情
        case 'QueryAboutBillFlow':
            queryAboutBillFlow.call(this, record);
            break;
        default:
            break;
    }
    /* 删除 */
    function singleDel(props, record, index) {
        const id = record[pkField].value;
        const ts = record.ts.value;
        const params = [
            {
                id,
                index
            }
        ];
        const paramInfoMap = { [id]: ts };

        ajax({
            url: url.commitUrl,
            data: {
                paramInfoMap,
                dataType: 'listData',
                OperatorType: 'DELETE',
                pageid: pagecode.replace('list', 'card'),
                commitType: 'commit' // 提交
            },
            success: (res) => {
                getScriptListReturn.call(
                    this,
                    params,
                    res,
                    props,
                    pkField,
                    tableId.replace('list', 'card'),
                    tableId,
                    true
                );
                // 清除缓存
                props.table.deleteCacheId(tableId, id);
            },
            error: (res) => {
                if (res && res.message) {
                    toast({ content: res.message, color: 'danger' });
                }
            }
        });
    }
    /* 审批详情 */
    function queryAboutBillFlow(record) {
        openListApprove(this, record, 'pk_inventory');
    }
    // 重新分配盘点人
    function reInventory(props, key, record, index) {
        const queryInfo = {};
        const tempUserdefObj = {
            pk_inventory: record.pk_inventory.value,
            signature: '7',
            inventoryWay: '1'
        };
        queryInfo.userdefObj = tempUserdefObj;
        ajax({
            url: url.queryAssignSavedUrl,
            data: queryInfo,
            success: (res) => {
                if (res.data) {
                    if (res.data.total == 'null') {
                        toast({
                            content: getMultiLangByID('201203004A-000034') /* 所有资产已盘点完毕，不支持重新分配！*/,
                            color: 'warning'
                        });
                    } else {
                        props.pushTo(cardRouter, {
                            status: 'edit',
                            current: '1',
                            id: record[pkField] ? record[pkField].value : '',
                            total_num: res.data.total,
                            pagecode: pagecode.replace('list', 'card'),
                            pk_org: record.pk_org.value
                        });
                    }
                }
            }
        });
    }
    /* 提交 收回 */
    function commitClick(OperatorType, props, tableId, index, record) {
        const paramInfoMap = {};
        paramInfoMap[record.pk_inventory.value] = record.ts.value;
        const params = [{ id: record.pk_inventory.value, index }];
        let commitType = '';
        if (OperatorType === 'SAVE') {
            // 提交传这个
            commitType = 'commit';
        }
        ajax({
            url: url.commitUrl,
            data: {
                paramInfoMap,
                dataType: 'listData',
                OperatorType,
                pageid: pagecodecard,
                commitType
            },
            success: (res) => {
                if (res && res.data && res.data.cardVos && res.data.cardVos.length > 0) {
                    // 处理盘点范围多语
                    res.data.cardVos.forEach((cardVo) => {
                        handleCheck_range.call(this, cardVo.head.card_head.rows[0].values.check_range, true);
                    });
                }
                getScriptListReturn.call(
                    this,
                    params,
                    res,
                    props,
                    pkField,
                    tableId.replace('list', 'card'),
                    tableId,
                    false,
                    pageConfig.dataSource
                );
            },
            error: (res) => {
                if (res && res.message) {
                    toast({ color: 'danger', content: res.message });
                }
            }
        });
    }
}

/*0GI1xcoeligdpMeXoHBphggJgy1ecejSkxa04FXn/MW9FwzMYLMeZK3mD3mLyGPh*/